package com.leetcode.hot100;

/**
 * @BelongsProject: LeetCode
 * @BelongsPackage: com.leetcode.hot100
 * @Author: lei
 * @CreateTime: 2024-03-20  09:11
 * @Description: TODO
 * @Version: 1.0
 */
public class 整数转罗马数字_12 {
    public static void main(String[] args) {
        System.out.println(intToRoman(10));

    }

    public static String intToRoman(int num) {
        int start = num / 1000;
        int num1 = num - (start * 1000);
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < start; i++) {
            stringBuilder.append("M");
        }
        int length = String.valueOf(num).length();
        int[] ints = new int[length];
        for (int i = 0; i < length; i++) {
            int temp = (int) Math.pow(10, i);
            ints[i] = (num1 / temp) % (10);
        }
        //System.out.println(Arrays.toString(ints));
        for (int i = length - 1; i >= 0; i--) {
            int pow = (int) Math.pow(10, i);
            int temp = ints[i] * pow;
            if (ints[i] != 4 && ints[i] != 9) {
                if (ints[i] >= 5) {
                    int j1 = ints[i] - 5;
                    process01(i, stringBuilder);
                    process02(j1, i, stringBuilder);
                } else {
                    process02(ints[i], i, stringBuilder);
                }
            } else {
                process(temp, stringBuilder);
            }
        }
        return stringBuilder.toString();
    }

    private static void process01(int i, StringBuilder stringBuilder) {
        switch (i) {
            case 0:
                stringBuilder.append("V");
                break;
            case 1:
                stringBuilder.append("L");
                break;
            case 2:
                stringBuilder.append("D");
                break;
        }
    }

    private static void process02(int j, int i, StringBuilder stringBuilder) {
        switch (i) {
            case 0:
                for (int i1 = 0; i1 < j; i1++) {
                    stringBuilder.append("I");
                }
                break;
            case 1:
                for (int i1 = 0; i1 < j; i1++) {
                    stringBuilder.append("X");
                }
                break;
            case 2:
                for (int i1 = 0; i1 < j; i1++) {
                    stringBuilder.append("C");
                }
                break;
        }
    }


    private static void process(int temp, StringBuilder stringBuilder) {
        switch (temp) {
            case 4:
                stringBuilder.append("IV");
                break;
            case 9:
                stringBuilder.append("IX");
                break;
            case 40:
                stringBuilder.append("XL");
                break;
            case 90:
                stringBuilder.append("XC");
                break;
            case 400:
                stringBuilder.append("CD");
                break;
            case 900:
                stringBuilder.append("CM");
                break;
        }
    }
}
